#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node
{
    int u,d;
}a[N];
bool cmp(node a,node b)
{
    if(a.u<a.d)
    {
        if(b.u<b.d) return a.u<b.u;
        return 1;
    }
    if(b.u<b.d) return 0;
    return a.d>b.d;
}
int n,up[N],down[N],ans;
int main() 
{
    #ifdef LOCAL
        freopen("data.in", "r", stdin);
        freopen("data.out", "w", stdout);
        auto start_time = chrono::high_resolution_clock::now();
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    // ================ 你的代码从这里开始 ================
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i].u>>a[i].d;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++) up[i]=up[i-1]+a[i].u;
    for(int i=1;i<=n;i++) down[i]=max(down[i-1],up[i])+a[i].d;
    ans=down[n];
    cout<<ans<<endl;
    // ================ 你的代码到这里结束 ================
    #ifdef LOCAL
        auto end_time = chrono::high_resolution_clock::now();
        auto duration = chrono::duration_cast<chrono::milliseconds>(end_time - start_time);
        cerr << "Time cost: " << duration.count() << " ms" << endl;
    #endif
    
    return 0;
}